<template>
    <el-dialog
    :title="!dataForm.id? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="100px">
      <el-form-item label="订单号" prop="sn">
        <el-input v-model="dataForm.sn" placeholder="订单号"></el-input>
      </el-form-item>
      <el-form-item label="订单金额" prop="orderAmount">
        <el-input v-model="dataForm.orderAmount" placeholder="订单金额"></el-input>
      </el-form-item>
      <el-form-item label="现金支付金额" prop="payAmount">
        <el-input v-model="dataForm.payAmount" placeholder="现金支付金额"></el-input>
      </el-form-item>
      <el-form-item label="充值卡余额抵扣金额" prop="balanceAmount">
        <el-input v-model="dataForm.balanceAmount" placeholder="充值卡余额抵扣金额"></el-input>
      </el-form-item>
      <el-form-item label="次数消耗" prop="timesCost">
        <el-input v-model="dataForm.timesCost" placeholder="次数消耗"></el-input>
      </el-form-item>
      <el-form-item label="积分消耗" prop="potionsCost">
        <el-input v-model="dataForm.potionsCost" placeholder="积分消耗"></el-input>
      </el-form-item>
      <el-form-item label="积分抵扣金额" prop="potionsAmount">
        <el-input v-model="dataForm.potionsAmount" placeholder="积分抵扣金额"></el-input>
      </el-form-item>
      <el-form-item label="订单赠送积分" prop="potionsGive">
        <el-input v-model="dataForm.potionsGive" placeholder="订单赠送积分"></el-input>
      </el-form-item>
      <el-form-item label="会员卡id" prop="vipCardId">
        <el-input v-model="dataForm.vipCardId" placeholder="会员卡id"></el-input>
      </el-form-item>
      <el-form-item label="会员卡号" prop="vipCardNo">
        <el-input v-model="dataForm.vipCardNo" placeholder="会员卡号"></el-input>
      </el-form-item>
      <el-form-item label="会员卡类型" prop="vipCardType">
        <el-input v-model="dataForm.vipCardType" placeholder="会员卡类型"></el-input>
      </el-form-item>
      <el-form-item label="会员id" prop="memberId">
        <el-input v-model="dataForm.memberId" placeholder="会员id"></el-input>
      </el-form-item>
      <el-form-item label="会员姓名" prop="memberName">
        <el-input v-model="dataForm.memberName" placeholder="会员姓名"></el-input>
      </el-form-item>
      <el-form-item label="会员电话" prop="memberMobile">
        <el-input v-model="dataForm.memberMobile" placeholder="会员电话"></el-input>
      </el-form-item>
      <el-form-item label="所属公司id" prop="companyId">
        <el-input v-model="dataForm.companyId" placeholder="所属公司id"></el-input>
      </el-form-item>
      <el-form-item label="所属公司名称" prop="companyName">
        <el-input v-model="dataForm.companyName" placeholder="所属公司名称"></el-input>
      </el-form-item>
      <el-form-item label="收银店铺id" prop="shopId">
        <el-input v-model="dataForm.shopId" placeholder="收银店铺id"></el-input>
      </el-form-item>
      <el-form-item label="收银店铺名称" prop="shopName">
        <el-input v-model="dataForm.shopName" placeholder="收银店铺名称"></el-input>
      </el-form-item>
      <el-form-item label="操作员" prop="operator">
        <el-input v-model="dataForm.operator" placeholder="操作员"></el-input>
      </el-form-item>
      <el-form-item label="收银时间" prop="createTime">
        <el-input v-model="dataForm.createTime" placeholder="收银时间"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" :disabled="disabled" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        disabled: false,
        dataForm: {
          id: null,
          sn: null,
          orderAmount: null,
          payAmount: null,
          balanceAmount: null,
          timesCost: null,
          potionsCost: null,
          potionsAmount: null,
          potionsGive: null,
          vipCardId: null,
          vipCardNo: null,
          vipCardType: null,
          memberId: null,
          memberName: null,
          memberMobile: null,
          companyId: null,
          companyName: null,
          shopId: null,
          shopName: null,
          operator: null,
          createTime: null
        },
        dataRule: {
          sn: [
            {required: true, message: '订单号不能为空', trigger: 'blur'}
          ],
          orderAmount: [
            {required: true, message: '订单金额不能为空', trigger: 'blur'}
          ],
          payAmount: [
            {required: true, message: '现金支付金额不能为空', trigger: 'blur'}
          ],
          balanceAmount: [
            {required: true, message: '充值卡余额抵扣金额不能为空', trigger: 'blur'}
          ],
          timesCost: [
            {required: true, message: '次数消耗不能为空', trigger: 'blur'}
          ],
          potionsCost: [
            {required: true, message: '积分消耗不能为空', trigger: 'blur'}
          ],
          potionsAmount: [
            {required: true, message: '积分抵扣金额不能为空', trigger: 'blur'}
          ],
          potionsGive: [
            {required: true, message: '订单赠送积分不能为空', trigger: 'blur'}
          ],
          vipCardId: [
            {required: true, message: '会员卡id不能为空', trigger: 'blur'}
          ],
          vipCardNo: [
            {required: true, message: '会员卡号不能为空', trigger: 'blur'}
          ],
          vipCardType: [
            {required: true, message: '会员卡类型不能为空', trigger: 'blur'}
          ],
          memberId: [
            {required: true, message: '会员id不能为空', trigger: 'blur'}
          ],
          memberName: [
            {required: true, message: '会员姓名不能为空', trigger: 'blur'}
          ],
          memberMobile: [
            {required: true, message: '会员电话不能为空', trigger: 'blur'}
          ],
          companyId: [
            {required: true, message: '所属公司id不能为空', trigger: 'blur'}
          ],
          companyName: [
            {required: true, message: '所属公司名称不能为空', trigger: 'blur'}
          ],
          shopId: [
            {required: true, message: '收银店铺id不能为空', trigger: 'blur'}
          ],
          shopName: [
            {required: true, message: '收银店铺名称不能为空', trigger: 'blur'}
          ],
          operator: [
            {required: true, message: '操作员不能为空', trigger: 'blur'}
          ],
          createTime: [
            {required: true, message: '收银时间不能为空', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || null;
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields();
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/order/order/detail/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.sn = data.item.sn;
                this.dataForm.orderAmount = data.item.orderAmount;
                this.dataForm.payAmount = data.item.payAmount;
                this.dataForm.balanceAmount = data.item.balanceAmount;
                this.dataForm.timesCost = data.item.timesCost;
                this.dataForm.potionsCost = data.item.potionsCost;
                this.dataForm.potionsAmount = data.item.potionsAmount;
                this.dataForm.potionsGive = data.item.potionsGive;
                this.dataForm.vipCardId = data.item.vipCardId;
                this.dataForm.vipCardNo = data.item.vipCardNo;
                this.dataForm.vipCardType = data.item.vipCardType;
                this.dataForm.memberId = data.item.memberId;
                this.dataForm.memberName = data.item.memberName;
                this.dataForm.memberMobile = data.item.memberMobile;
                this.dataForm.companyId = data.item.companyId;
                this.dataForm.companyName = data.item.companyName;
                this.dataForm.shopId = data.item.shopId;
                this.dataForm.shopName = data.item.shopName;
                this.dataForm.operator = data.item.operator;
                this.dataForm.createTime = data.item.createTime;
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.disabled = true;
            this.$http({
                url: this.$http.adornUrl(`/order/order/${!this.dataForm.id ? 'save' : 'update'}`),
                method: 'post',
                data: this.$http.adornData({
                  id: this.dataForm.id || undefined,
                  sn: this.dataForm.sn,
                  orderAmount: this.dataForm.orderAmount,
                  payAmount: this.dataForm.payAmount,
                  balanceAmount: this.dataForm.balanceAmount,
                  timesCost: this.dataForm.timesCost,
                  potionsCost: this.dataForm.potionsCost,
                  potionsAmount: this.dataForm.potionsAmount,
                  potionsGive: this.dataForm.potionsGive,
                  vipCardId: this.dataForm.vipCardId,
                  vipCardNo: this.dataForm.vipCardNo,
                  vipCardType: this.dataForm.vipCardType,
                  memberId: this.dataForm.memberId,
                  memberName: this.dataForm.memberName,
                  memberMobile: this.dataForm.memberMobile,
                  companyId: this.dataForm.companyId,
                  companyName: this.dataForm.companyName,
                  shopId: this.dataForm.shopId,
                  shopName: this.dataForm.shopName,
                  operator: this.dataForm.operator,
                  createTime: this.dataForm.createTime
                })
            }).then(({data}) => {
              this.disabled = false;
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false;
                    this.$emit('refreshDataList');
                  }
                })
              } else {
                this.$message.error(data.msg);
              }
            })
          }
        })
      }
    }
  }
</script>
